package com.visikard.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.visikard.application.Constant;
import com.visikard.application.MyApplication;
import com.visikard.application.UIMessage;
import com.visikard.ui.R;

@Deprecated
public class TransactionPlaceFragment extends BaseFragment {
	
	private EditText edtLocation;
	String location = "";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View parent = inflater.inflate(R.layout.transaction_place_fragment,
				container, false);
		
		edtLocation = (EditText) parent.findViewById(R.id.edt_transaction_place);

		edtLocation.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {

				if (actionId == EditorInfo.IME_ACTION_DONE) {
					showKeyboard(false);
					location = edtLocation.getText().toString().trim();
					if(location.length() > 0){
						Constant.locationAddress = location;
						MyApplication.instance.saveSessionAddress(location);
					}else
						Constant.locationAddress = "";
					
					mHandler.sendEmptyMessage(UIMessage.MSG_SAVE_ADDRESS_SUCCESS);
				}
				return false;
			}
		});
		
		return parent;
	}
	
	private void showKeyboard(boolean show) {
		if (show)
			getActivity().getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		else
			getActivity().getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case UIMessage.MSG_SAVE_ADDRESS_SUCCESS:
			Toast.makeText(getActivity(), R.string.transaction_place_save_address_success,
					Toast.LENGTH_LONG).show();
			return true;
		}
		return super.handleMessage(msg);
	}
	
}
